<?php
error_reporting(0);
/**
 * GameController
 * 
 * @author
 * @version 
 */
require_once 'Zend/Controller/Action.php';
class ToolController extends Sunnet_Controller_Action
{
    var $_arrParam;
    protected $domain = DOMAIN;
    public function init()
    {
        //      Truyền các tham số nhận được ra view
        $this->_arrParam = $this->_request->getParams();
        if (empty($this->_arrParam['page'])) {
            $this->_arrParam['page'] = 1;
        }
        if (empty($this->_arrParam['line_per_page'])) {
            $this->_arrParam['line_per_page'] = 10;
        }

        $this->view->arrParam = $this->_arrParam;
        $this->view->domain = $this->domain;
        $this->view->function = $this;
    }

    public function preDispatch(){
        $template_path = TEMPLATE_PATH . "/wap/default/";
        $this->loadTemplate($template_path,'template.ini','template');
         //set group main
        Zend_Loader::loadClass('Wap_Models_ControllerModel');
        $controllerModel = new Wap_Models_ControllerModel();       
        $this->view->groupmain_list = $controllerModel->listMenuGroupNote();
        $this->view->newsHots = $controllerModel->listNewsHotRight();
        $this->view->listBanners = $controllerModel->listBannerHeader();
    }

    public function indexAction (){
//      Load js và css
        $this->view->headLink()->appendStylesheet('/css/style.css','screen');
        $this->view->headLink()->appendStylesheet('/css/verify.css','screen');

        //lay quan huyen ra
        Zend_Loader::loadClass('Verify_Models_TblQuanModel');
        $qh_model= new Verify_Models_TblQuanModel();
        $groupquanhuyen_list = $qh_model->getListQuanHuyen();
        $this->view->groupquanhuyen_list = $groupquanhuyen_list;
        //list cac post ra
        if ($this->_request->isPost()){
            $param= $this->_request->getParams();
             $sotienvay = intval(str_replace(",",'',trim($param ['sotienvay'])));
             $thoigianvay = intval(str_replace(",",'',trim($param ['thoigianvay'])));
             $laixuat = 0.0125;
             $hid_sotienhangthang = intval(str_replace(",",'',trim($param ['hid_sotienhangthang'])));

             $luong = intval(str_replace(",",'',trim($param ['luong'])));
             $thuenhaxe = intval(str_replace(",",'',trim($param ['thuenhaxe'])));
             $hokinhdoanh = intval(str_replace(",",'',trim($param ['hokinhdoanh']))); 
             $doanhnghieprieng = intval(str_replace(",",'',trim($param ['doanhnghieprieng']))); 
             $thunhapkhac = intval(str_replace(",",'',trim($param ['thunhapkhac']))); 

             // tai san dam bao va co dinh
             $tsdb1 = intval(str_replace(",",'',trim($param ['tsdb1']))); 
             $tsdb2 = intval(str_replace(",",'',trim($param ['tsdb2']))); 
             $tsdb3 = intval(str_replace(",",'',trim($param ['tsdb3']))); 

             // tai san dam bao va co dinh
             $tscd1 = intval(str_replace(",",'',trim($param ['tscd1']))); 
             $tscd2 = intval(str_replace(",",'',trim($param ['tscd2']))); 
             $tscd3 = intval(str_replace(",",'',trim($param ['tscd3']))); 

             $loai_tscd1 = intval($param ['loai_tscd1']);
             $loai_tscd2 = intval($param ['loai_tscd2']);
             $loai_tscd3 = intval($param ['loai_tscd3']);

             $quanhuyen1 = intval($param ['quanhuyen1']);
             $quanhuyen2 = intval($param ['quanhuyen2']);
             $quanhuyen3 = intval($param ['quanhuyen3']);
             // tao mang ngan hang phu hop voi dk
             $arrNganHang = array();

             //duy qua tung ngan hang 
             Zend_Loader::loadClass('Verify_Models_TblBankModel');
                $nganHang_model= new Verify_Models_TblBankModel();
                $groupquanhuyen_list = $nganHang_model->getListNganHang();


                foreach ($groupquanhuyen_list as $key => $groupquanhuyen) {
                    // check xem tai san co o dung noi can the chap hay ko
                    $quanhuyen_bank =  $groupquanhuyen['quanhuyen'];
                    $quanhuyen_bank = explode(',', $quanhuyen_bank);
                    //cho mac dinh la ko dat yeu cau
                    $checkok = 0;
                    //kiem tra thu nhap xem co dat yeu cau - buoc 1
                         $luong_bank = intval($luong*$groupquanhuyen['luong']);
                         $thuenhaxe_bank = intval($thuenhaxe*$groupquanhuyen['thuenhaxe']);
                         $hokinhdoanh_bank = intval($hokinhdoanh*$groupquanhuyen['hokinhdoanh']);
                         $doanhnghieprieng_bank = intval($doanhnghieprieng*$groupquanhuyen['doanhnghieprieng']);
                         $thunhapkhac_bank = intval($thunhapkhac*$groupquanhuyen['thunhapkhac']);
                        //tinh toan buoc thu nhap - b1
                         $tongthunhap_b1 = $luong_bank+$thuenhaxe_bank+$hokinhdoanh_bank+$doanhnghieprieng_bank+$thunhapkhac_bank;


                         $thunhapchungminh = $sotienvay*($laixuat*100)+9100000; 
                        if($tongthunhap_b1 < $thunhapchungminh) {
                            $checkok = 0;
                        }else {
                            //thoa man yeu cau buoc 1 chuyen qua buoc 2
                            //kiem tra tai khoan bao dam
                            $tsdb1 = intval($tsdb1*$groupquanhuyen['tsdb']);
                            // $tsdb2 = intval($tsdb2*$groupquanhuyen['tsdb']);
                            // $tsdb3 = intval($tsdb3*$groupquanhuyen['tsdb']);
                            $tsdb2 = 0; //chua dung
                            $tsdb3 = 0;

                            $tong_tsdb = $tsdb1 + $tsdb2 + $tsdb3;

                            //kiem tra tai khoan co dinh
                            if($loai_tscd1==0) $loai1 = $groupquanhuyen['cscd_sodo'];
                            else $loai1 = $groupquanhuyen['cscd_hopdong'];

                            if($loai_tscd2==0) $loai2 = $groupquanhuyen['cscd_sodo'];
                            else $loai2 = $groupquanhuyen['cscd_hopdong'];

                            if($loai_tscd3==0) $loai3 = $groupquanhuyen['cscd_sodo'];
                            else $loai3 = $groupquanhuyen['cscd_hopdong'];
                            
                            // neu quan huyen thuoc noi ngan hang cung cap ko                  
                            if (in_array($quanhuyen1, $quanhuyen_bank)) { //thuoc quan huyen thi cho la 100%         
                                $tscd_thu1 = intval($tscd1*$loai1);
                            }else { // neu khong thuoc thi lai bang tinh_quanhuyen
                                $tscd_thu1 = intval($tscd1*$groupquanhuyen['tinh_quanhuyen']*$loai1);
                            }

                            if (in_array($quanhuyen2, $quanhuyen_bank)) { //thuoc quan huyen thi cho la 100%         
                                $tscd_thu2 = intval($tscd2*$loai2);
                            }else { // neu khong thuoc thi lai bang tinh_quanhuyen
                                $tscd_thu2 = intval($tscd2*$groupquanhuyen['tinh_quanhuyen']*$loai2);
                            }

                            if (in_array($quanhuyen3, $quanhuyen_bank)) { //thuoc quan huyen thi cho la 100%         
                                $tscd_thu3 = intval($tscd3*$loai3);
                            }else { // neu khong thuoc thi lai bang tinh_quanhuyen
                                $tscd_thu3 = intval($tscd3*$groupquanhuyen['tinh_quanhuyen']*$loai3);
                            }

                            $tong_tscd = $tscd_thu1 + $tscd_thu2 + $tscd_thu3;
                            // XEM LAI
                            $tongthunhap_b2 = $tong_tsdb+$tong_tscd;                        
                            $tinhtoanlai_tongthunhap_b2 = $tongthunhap_b2*0.6;

                            if($tinhtoanlai_tongthunhap_b2>=$sotienvay) $checkok = 1; // dieu kien qua ca 2 buoc thi cho no = 1 la thoa man
                            else $checkok = 0;
                        }
                    //Cho cac ngan hang phu hop voi y cau vao mang array
                    //day ngan hang phu hop voi yeu cau vao mang
                    if($checkok==1) array_push($arrNganHang, $groupquanhuyen['id']);  

                } // end foreach
                $this->view->arrNganHang = $arrNganHang;
        }

        //
    }

    public function soTienVayToiDaAction (){
//      Load js và css
        $this->view->headLink()->appendStylesheet('/css/style.css','screen');
        $this->view->headLink()->appendStylesheet('/css/verify.css','screen');

        

    }

    public function bangTinhDongTienAction (){
//      Load js và css
        $this->view->headLink()->appendStylesheet('/css/style.css','screen');
        $this->view->headLink()->appendStylesheet('/css/verify.css','screen');
        
                
        if ($this->_request->isPost()){
            $param= $this->_request->getParams();

            $sotienvay = intval(str_replace(",",'',trim($param ['sotienvay'])));
            $thoigianvay = intval(str_replace(",",'',trim($param ['thoigianvay'])));
            $laisuat = (float) trim($param ['laisuat']);
            $laisuat = $laisuat/100;
            
            
            $ngaytrano = explode("/", $param ['ngaytrano']);
            $day = $ngaytrano[1];
            $month = $ngaytrano[0];
            $year = $ngaytrano[2];

            $hid_sotienhangthang = intval(str_replace(",",'',trim($param ['hid_sotienhangthang'])));

             $this->view->sotienvay = $sotienvay;
             $this->view->thoigianvay = $thoigianvay;
             $this->view->laisuat = $laisuat;
             $this->view->day = $day;
             $this->view->month = $month;
             $this->view->year = $year;
             $this->view->hid_sotienhangthang = $hid_sotienhangthang;

        }


    }

    public function dangKyHoSoOnlineAction (){
//      Load js và css
        $this->view->headLink()->appendStylesheet('/css/style.css','screen');
        $this->view->headLink()->appendStylesheet('/css/verify.css','screen');

        $auth = Zend_Auth::getInstance();
        if ($auth->hasIdentity()){
            $identity = $auth->getIdentity();
            $user_id = $identity['UserID'];
        }else{
            // chuyen sang trang thong bao
            $this->_redirect('/account/dang-nhap');  
        }

        //insert vao database
        if ($this->_request->isPost()){
            $param= $this->_request->getParams();
            $UserID = $user_id;

            $hovaten = trim($param ['hovaten']);
            $email = trim($param ['email']);
            $sodienthoai = trim($param ['sodienthoai']);
            $tglienlac = trim($param ['tglienlac']);
            $cmnd = trim($param ['cmnd']);
            $ngaycap = trim($param ['ngaycap']);
            $noicap = trim($param ['noicap']);

             $sotienvay = intval(str_replace(",",'',trim($param ['sotienvay'])));
             $thoigianvay = intval(str_replace(",",'',trim($param ['thoigianvay'])));

             $luong = intval(str_replace(",",'',trim($param ['luong'])));
             $thuenhaxe = intval(str_replace(",",'',trim($param ['thuenhaxe'])));
             $hokinhdoanh = intval(str_replace(",",'',trim($param ['hokinhdoanh']))); 
             $doanhnghieprieng = intval(str_replace(",",'',trim($param ['doanhnghieprieng']))); 
             $thunhapkhac = intval(str_replace(",",'',trim($param ['thunhapkhac']))); 

             $tsdb1 = intval(str_replace(",",'',trim($param ['tsdb1']))); 
             $tscd1 = intval(str_replace(",",'',trim($param ['tscd1']))); 
             $quanhuyen = trim($param ['quanhuyen']);
             $loai_tscd1 = trim($param ['loai_tscd1']);
             $duan = trim($param ['duan']);
             
             //tao ma so random
             $validator1 = new Zend_Validate_Db_RecordExists(
                                array(
                                    'table' => 'tbl_hoso_online',
                                    'field' => 'maso'
                                )
             );
             do{
                $code = $this->generateCode(10);
             }while ($validator1->isValid($code));
             $maso = $code;
             $namehoso = "Hồ sơ ngày ".date("d-m-Y")." mã số ".$maso;
             //insert
             Zend_Loader::loadClass('Wap_Models_HoSoOnlineModel');
             $db= new Wap_Models_HoSoOnlineModel();
                                            
                                            $data= array(
                                                    'UserID'=>  $UserID,
                                                    'maso' => $maso,
                                                    'namehoso' => $namehoso,

                                                    'hovaten'=>  $hovaten,
                                                    'email' => $email,
                                                    'sodienthoai' => $sodienthoai,
                                                    'tglienlac'=>  $tglienlac,
                                                    'cmnd' => $cmnd,
                                                    'ngaycap' => $ngaycap,
                                                    'noicap' => $noicap,

                                                    'sotienvay' => $sotienvay,
                                                    'thoigianvay'=>$thoigianvay,

                                                    'luong'=>$luong,
                                                    'thuenhaxe'=>$thuenhaxe,
                                                    'hokinhdoanh'=>$hokinhdoanh,    
                                                    
                                                    'doanhnghieprieng'=>$doanhnghieprieng,
                                                    'thunhapkhac'=>$thunhapkhac,                                                
                                                    'quanhuyen'=>$quanhuyen,
                                                    'tsdb1'=>$tsdb1,  
                                                    'tscd1'=>$tscd1,
                                                    'loai_tscd1'=>$loai_tscd1,                                                
                                                    'duan'=>$duan,
                                                    'startdate'=>$startdate,  
                                            );

                                            try{
                                                $result = $db->add($data);
                                            }catch(Exception $e)
                                            {
                                                echo $e;
                                            }

            if($result){
                $this->_redirect('/tool/dang-ky-ho-so-online-complate');  
            }
        } //if post


    }

    public function dangKyHoSoOnlineComplateAction (){
//      Load js và css
        $this->view->headLink()->appendStylesheet('/css/style.css','screen');
        $this->view->headLink()->appendStylesheet('/css/verify.css','screen');

        $auth = Zend_Auth::getInstance();
        if ($auth->hasIdentity()){
            $identity = $auth->getIdentity();
            $user_id = $identity['UserID'];
        }else{
            // chuyen sang trang thong bao
            $this->_redirect('/account/dang-nhap');  
        }

    }

    public function timNganHangAction (){
//      Load js và css
        $this->view->headLink()->appendStylesheet('/css/style.css','screen');
        $this->view->headLink()->appendStylesheet('/css/verify.css','screen');

        //lay quan huyen ra
        Zend_Loader::loadClass('Verify_Models_TblQuanModel');
        $qh_model= new Verify_Models_TblQuanModel();
        $groupquanhuyen_list = $qh_model->getListQuanHuyen();
        $this->view->groupquanhuyen_list = $groupquanhuyen_list;

        //lay du an
        Zend_Loader::loadClass('Wap_Models_DuAnModel');
        $da_model= new Wap_Models_DuAnModel();
        $duan_lists = $da_model->getListDuAn();
        $this->view->duan_lists = $duan_lists;

        Zend_Loader::loadClass('Verify_Models_TblBankModel');
        $nganHang_model= new Verify_Models_TblBankModel();

        $checkpost="";
        $checkpost1="";
        $checkpost2="";
        $checkpost3="";
        if ($this->_request->isPost()){
            $param= $this->_request->getParams();
            //search du an
            if(isset($param['duan'])){
                $id_duan = intval($param['duan']);
                $nganhang_duans = $nganHang_model->searchNganHangByDuAn($id_duan);
                //Zend_Debug::dump($nganhang_duans); die();
                $this->view->nganhang_duans = $nganhang_duans;

                $checkpost="post";            
            }

            if(isset($param['quanhuyen1'])){
                $loaitaisan = intval($param['loaitaisan']);
                $quanhuyen1 = intval($param['quanhuyen1']);
                
                $nganhang_tsbdfs = $nganHang_model->searchTaiSanDamBao($loaitaisan, $quanhuyen1);
                //Zend_Debug::dump($nganhang_duans); die();
                $this->view->nganhang_tsbdfs = $nganhang_tsbdfs;

                $checkpost1="post";                 
            }

            if(isset($param['sangtenso'])){                
                $sangtenso = intval($param['sangtenso']);
                
                $nganhang_sangtensofs = $nganHang_model->searchSangTenSoByDuAn($sangtenso);                
                //Zend_Debug::dump($nganhang_duans); die();
                $this->view->nganhang_sangtensofs = $nganhang_sangtensofs;

                $checkpost2="post";                 
            }

            if(isset($param['mdsudung'])){                
                $mdsudung = intval($param['mdsudung']);
                
                $nganhang_mucdichsudungfs = $nganHang_model->searchMDSuDungByDuAn($mdsudung);
                //Zend_Debug::dump($nganhang_mucdichsudungfs); die();
                $this->view->nganhang_mucdichsudungfs = $nganhang_mucdichsudungfs;

                $checkpost3="post";                
            }

                      
        } //end post
        $this->view->checkpost = $checkpost;
        $this->view->checkpost1 = $checkpost1;
        $this->view->checkpost2 = $checkpost2;
        $this->view->checkpost3 = $checkpost3;
    }


    //FUNCTION
    function generateCode($characters) {
        // $characters = how many digits or characters to return.
        // You can use any set of characters you want.
        $possible = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ';
        $code = '';
        $i = 0;
        while ($i < $characters) { 
            $code .= substr($possible, mt_rand(0, strlen($possible)-1), 1);
            $i++;
        }
        return $code;
    }

    public function getNganHangByID ($id){
        Zend_Loader::loadClass('Verify_Models_TblBankModel');
                $nganHang_model= new Verify_Models_TblBankModel();
                $nganhang = $nganHang_model->getIDNganHang($id);
        return $nganhang;
    }

    //code date time
    public function convertTimeAgo ($activity_time){       
       $timeinsecond = time() - $activity_time;
       if($timeinsecond >=0){
         $minus  = abs(floor(($timeinsecond%3600)/60));
         $hour   =   abs(round(($timeinsecond-$minus*60)/3600,0));              
          
         $timematch = 0;
         if($timeinsecond<60){
            $timematch = $timeinsecond." giây trước";  
            return $timematch;
         }elseif(60<$timeinsecond && $timeinsecond<3600){
            $timematch = $minus." phút trước";      
            return $timematch;       
         }elseif(3600<=$timeinsecond && $timeinsecond<86400){
            $timematch = $hour." giờ trước"; 
            return $timematch;
         }elseif(86400<=$timeinsecond && $timeinsecond<518400){
            $timematch =  abs(round(($hour/24))) . " ngày trước";
            return $timematch;
         }else {
            $timematch= date('d/m/Y', $activity_time);
            return $timematch;
         }
         return $timematch;
       }
    }
    function cut_substr($str,$n)
    {       
        $str = strip_tags($str);   
        $str = trim($str);    
       // ----- remove control characters ----- 

        $str = str_replace("\r", '<br/>', $str);    // --- replace with empty space
        $str = str_replace("\n", '', $str);   // --- replace with space
        $str = str_replace("\t", '', $str);   // --- replace with space
        // ----- remove cac br lien tiep ----- 
        $str = preg_replace('#(<br */?>\s*)+#i', '<br />', $str);

        $html = substr($str,0,$n);
        $html = substr($html,0,strrpos($html,' '));

        return $html.'...';
    }  

    function cut_substr_strip($str)
    {                   
        $str = strip_tags($str);   
        $str = trim($str);    
       // ----- remove control characters ----- 

        $str = str_replace("\r", '<br/>', $str);    // --- replace with empty space
        $str = str_replace("\n", '', $str);   // --- replace with space
        $str = str_replace("\t", '', $str);   // --- replace with space
        // ----- remove cac br lien tiep ----- 
        $str = preg_replace('#(<br */?>\s*)+#i', '<br />', $str);

        return $str;

    }  

}
